// JavaScript Document
var flag = 1;
function searchToConfirmList(pageNum) {
    $('tr[name="toConfirmData"]').remove();
    var param = {};
    param.startDate = $("#startDate").val();
    param.endDate = $("#endDate").val();
    // 申请人id
    param.applyUserId = $("#user").val();

    param.pageNum = pageNum;

    var pageResult = doAjax(to_confirm_expense_applys, param);
    if(pageResult.t && pageResult.t.length != 0){
        // 如果有数据，则加载页面信息
        $.each(pageResult.t, function (index, value) {
            var tr ="<tr name='toConfirmData'>"+
                "<td class=\"txt\"><input type='checkbox' name='toConfirmCheck'></td>"+
                "<td class=\"txt\" expenseApplyId="+value.id+" style=\"display:none\"></td>"+
                "<td class=\"txt\">"+value.goodsName+"</td>"+
                "<td class=\"txt\">"+value.price+"</td>"+
                "<td class=\"txt\">"+value.applyUserName+"</td>"+
                "<td class=\"txt\">"+value.createTime+"</td>"+
                "<td class=\"txt\">"+value.remark+"</td>"+
                "</tr>"
            $('#applyList').append(tr);
        });
    }
    $('#box').paging({
        initPageNo: pageResult.pageNum, // 初始页码
        totalPages: pageResult.pageCount, //总页数
        totalCount: '合计' + pageResult.total + '条数据', // 条目总数
        slideSpeed: 600, // 缓动速度。单位毫秒
        jump: false, //是否支持跳转
        callback: function(page) { // 回调函数
            searchToConfirmList(page);
        }
    });
}
function selectAll(){
    var trs = $("tr[name='toConfirmData']");
    if(trs.length == 0){
        return;
    }
    if(flag){
        trs.each(function () {
            $(this).children('td').eq(0).find('input').attr('checked',true);
        });
        flag = 0;
    }else {
        trs.each(function () {
            $(this).children('td').eq(0).find('input').attr('checked',false);
        });
        flag = 1;
    }

}

function execute(status){
    if(confirm("确认审核/驳回报销申请吗") == true){
        var trs = $("tr[name='toConfirmData']");
        if(trs.length == 0){
            alert("没有需要审核的申请");
            return;
        }
        var ids = "";
        trs.each(function () {
            if($(this).children('td').eq(0).find('input').attr('checked')){
                ids = ids + $(this).children('td').eq(1).attr("expenseApplyId")+",";
            }
        });
        var param = {};
        if(ids == ""){
            alert("请选择审核的数据");
            return;
        }
        param.ids = ids.substring(0, ids.length-1);
        param.status = status;
        var result = doAjaxAll(confirm_expensy_applys, param);
        if(result.code == 1){
            alert("操作成功！");
            location.reload();
        }
    }
}
$(function(){
    // 初始化报销数据
    var initData = doAjaxOnlyUrl(to_confirm_users);
    var userList = initData.userDTOList;
    // 设置申请店铺和申请人员
    $(".header ul li:eq(0) span").html(initData.storeName);
    $(".header ul li:eq(1) span").html(initData.userName);
    // 设置接收店铺和确认人员数据
    var user = $("#user");
    user.prepend("<option value='0'>全部</option>");
    if(userList && userList.length != 0){
        for(var i = 0; i < userList.length; i ++){
            user.append("<option value="+userList[i].id+">"+userList[i].name+"</option>");
        }
    }

    laydate.render({
        elem: '#endDate'
    });
    laydate.render({
        elem: '#startDate'
    });
    searchToConfirmList(1);

    // 确认报销
    $(".rit-btn").click(function () {
        execute(2);
    });
    // 驳回
    $(".exit-btn").click(function () {
        execute(3);
    });

});



























